#include "CCamera.h"


CCamera::CCamera(ISceneManager* smgr, CCar& carToFolow)
{
	this->smgr = smgr;
	car = &carToFolow;

	camera[CAM_FOLLOW] = smgr->addCameraSceneNode();
	camera[CAM_FREE] = smgr->addCameraSceneNodeFPS(0, 75.f, 0.05f);

	distance = 2.5f;
	camAngle = car->getAngle();

	activeCam = CAM_FOLLOW;
	smgr->setActiveCamera(camera[activeCam]);
}


CCamera::~CCamera(void)
{
}


void CCamera::update(void)
{
	camera[CAM_FOLLOW]->setTarget(car->getPosition());
	targetAngle = car->getAngle();

	if (car->getSpeed() < 0)
	{
		targetAngle -= 180;
	}

	if (camAngle != targetAngle)
	{
		followStep = (camAngle - targetAngle) / 40;
		camAngle -= followStep;
	}

	camera[CAM_FOLLOW]->setPosition(car->getPosition() + vector3df(
		distance * sin(camAngle * DEGTORAD), 1.f, distance * cos(camAngle * DEGTORAD)));
}


void CCamera::switchCam(void)
{
	if (activeCam == CAM_FOLLOW)
	{
		camera[CAM_FREE]->setPosition(car->getPosition());
		activeCam = CAM_FREE;
	}
	else
	{
		activeCam = CAM_FOLLOW;
	}

	smgr->setActiveCamera(camera[activeCam]);
}